package cn.edu.zafu.bigdata.lekang;

import java.util.Scanner;
import java.util.Random;

public class GuessNumberGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int rounds = 1;  // 游戏轮数
        boolean playAgain = true;

        while (playAgain) {
            System.out.println("欢迎来到猜数字游戏！");
            System.out.print("请输入你希望猜数字的范围上限（默认100）：");
            int maxNumber = scanner.hasNextInt() ? scanner.nextInt() : 100;
            System.out.print("请输入每轮游戏你希望的最大猜测次数（默认10）：");
            int maxAttempts = scanner.hasNextInt() ? scanner.nextInt() : 10;

            int numberToGuess = random.nextInt(maxNumber) + 1;
            int attempts = 0;
            boolean guessed = false;

            System.out.println("我已经想好了一个1到" + maxNumber + "之间的数字。");

            while (attempts < maxAttempts && !guessed) {
                System.out.print("请猜一个数字：");
                int guess = scanner.nextInt();
                attempts++;

                if (guess < numberToGuess) {
                    System.out.println("你猜的数字太小了！");
                } else if (guess > numberToGuess) {
                    System.out.println("你猜的数字太大了！");
                } else {
                    System.out.println("恭喜你，猜对了！");
                    guessed = true;
                }
            }

            if (!guessed) {
                System.out.println("很遗憾，你没有在限定次数内猜对。正确数字是：" + numberToGuess);
            }

            System.out.print("再来一局？(y/n)：");
            playAgain = scanner.next().equalsIgnoreCase("y");
            rounds++;
        }

        System.out.println("游戏结束，你总共玩了" + rounds + "轮。");
        scanner.close();
    }
}
